feat: 添加更多嵌入提供商,优化现有embeddingurl处理逻辑,添加ollama embedding提供商#6642
feat: 添加更多嵌入提供商,优化现有embeddingurl处理逻辑,添加ollama embedding提供商#6642whatevertogo wants to merge 11 commits intoAstrBotDevs:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求通过引入一个通用的 OpenAI 兼容 Embedding 提供商,显著扩展了 AstrBot 对嵌入服务的支持范围。它解决了现有实现中对非标准 API Base URL 处理的限制,并提供了灵活的配置选项,如 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- In
normalize_openai_compatible_embedding_api_base, a host without scheme (e.g.api.openai.com) is treated as a path and returned unchanged, which produces an invalid base URL; consider either auto-prefixinghttps://or explicitly validating and rejecting scheme-less values to avoid confusing runtime failures. - The new
send_dimensions_paramflag is parsed quite liberally viashould_send_dimensions_param(accepting various stringy truthy values); if config is only ever stored as a native boolean, you could simplify this to a direct bool read and avoid surprising behavior when unconventional strings are passed in.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `normalize_openai_compatible_embedding_api_base`, a host without scheme (e.g. `api.openai.com`) is treated as a path and returned unchanged, which produces an invalid base URL; consider either auto-prefixing `https://` or explicitly validating and rejecting scheme-less values to avoid confusing runtime failures.
- The new `send_dimensions_param` flag is parsed quite liberally via `should_send_dimensions_param` (accepting various stringy truthy values); if config is only ever stored as a native boolean, you could simplify this to a direct bool read and avoid surprising behavior when unconventional strings are passed in.
## Individual Comments
### Comment 1
<location path="tests/test_openai_compatible_embedding_source.py" line_range="72-81" />
<code_context>
assert data["status"] == "ok" and "platform" in data["data"]
+@pytest.mark.asyncio
+async def test_provider_template_exposes_openai_compatible_embedding_presets(
+ app: Quart,
</code_context>
<issue_to_address>
**suggestion (testing):** Add assertions that `terminate()` properly closes the underlying AsyncOpenAI client
Current tests call `terminate()` in `finally` blocks but never verify that the underlying client is closed. Since `_FakeAsyncOpenAI` tracks `closed`, please add assertions (e.g., `assert provider_without_dimensions.client.closed is True` or via `instances`) after `terminate()` to validate resource cleanup and catch regressions in connection handling.
</issue_to_address>
### Comment 2
<location path="docs/en/use/knowledge-base.md" line_range="25" />
<code_context>
+> `OpenAI Compatible Embedding` includes a `send_dimensions_param` switch. When enabled, AstrBot sends `embedding_dimensions` to the upstream embedding API as the `dimensions` parameter. Disable it for OpenAI-compatible services that only need the local vector size and do not support `dimensions`.
+
+> [!NOTE]
+> The Volcengine preset defaults to `doubao-embedding-vision`. AstrBot's knowledge-base pipeline is still text chunking plus text embedding only, so this integration uses the model with text input only and does not add multimodal knowledge-base support yet,although it is a multimodal embedding model.
+
+> [!NOTE]
</code_context>
<issue_to_address>
**issue (typo):** Add a space after the comma in "yet,although" to fix the typo.
Please insert a space after the comma so it reads "yet, although it is a multimodal embedding model."
```suggestion
> The Volcengine preset defaults to `doubao-embedding-vision`. AstrBot's knowledge-base pipeline is still text chunking plus text embedding only, so this integration uses the model with text input only and does not add multimodal knowledge-base support yet, although it is a multimodal embedding model.
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
本次 PR 新增了通用的 OpenAICompatibleEmbeddingProvider,并内置了智谱和火山引擎的预设,这是一个非常实用的功能增强,极大地扩展了嵌入服务的可选择范围。代码实现考虑周全,特别是对 API Base URL 的智能处理和 send_dimensions_param 开关的设计,显示了对不同服务兼容性的深入思考。同时,相关的单元测试、文档和国际化配置都一并更新,做得非常完整。
我在代码中发现了一些可以改进的地方,主要集中在新添加的 OpenAICompatibleEmbeddingProvider 中,涉及资源管理和配置解析的健壮性。具体的建议已在代码注释中提出,希望能帮助您进一步完善代码。
There was a problem hiding this comment.
Pull request overview
This PR adds a generic OpenAI-compatible embedding provider adapter (with smarter API base handling) and ships Zhipu and Volcengine embedding presets, plus corresponding Dashboard/i18n/docs and test coverage.
Changes:
- Introduce
openai_compatible_embeddingprovider with API base normalization and an opt-insend_dimensions_paramtoggle. - Add built-in provider templates/presets (OpenAI-compatible, Zhipu, Volcengine, Ollama) and expose them through the Dashboard.
- Add unit/integration tests and update docs + i18n strings.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
astrbot/core/provider/sources/openai_compatible_embedding_source.py |
New OpenAI-compatible embedding adapter (API base normalization + optional dimensions forwarding). |
astrbot/core/provider/manager.py |
Registers lazy import for the new provider type. |
astrbot/core/config/default.py |
Adds provider templates/presets and schema field for send_dimensions_param. |
dashboard/src/components/shared/AstrBotConfig.vue |
Simplifies how provider/item hints are surfaced in the config UI. |
dashboard/src/i18n/locales/zh-CN/features/config-metadata.json |
Adds hint/label strings for new embedding presets and send_dimensions_param. |
dashboard/src/i18n/locales/en-US/features/config-metadata.json |
Same i18n updates (English). |
dashboard/src/i18n/locales/ru-RU/features/config-metadata.json |
Same i18n updates (Russian). |
docs/zh/use/knowledge-base.md |
Documents new embedding options and the send_dimensions_param behavior. |
docs/en/use/knowledge-base.md |
English documentation update for the new embedding options. |
tests/test_openai_compatible_embedding_source.py |
Unit tests for API base normalization + dimensions forwarding behavior. |
tests/test_dashboard.py |
Dashboard API tests for templates exposure and embedding dim endpoint support. |
- Fix typo in docs: "yet,although" → "yet, although" - Fix resource leak: store httpx.AsyncClient as self._http_client and close in terminate() - Add timeout parsing with exception handling for invalid values - Handle API base URL without scheme (e.g., "api.openai.com") - Add test assertions to verify terminate() properly closes client
- Clarify that openai_embedding is kept for backward compatibility - Explain dimensions forwarding behavior difference between the two providers - Update hints in en-US, zh-CN, and ru-RU locales
- Explain that dimensions enables dimension reduction (not just forwarding) - List services that support it: OpenAI, Zhipu, Volcengine - Update all three locales: en-US, zh-CN, ru-RU
Replace hardcoded strings with i18n key paths to enable proper translation in the frontend for send_dimensions_param field.
|
为什么没有合并 |
|
我觉得引入Ollama API 可以很大程度上改善性能以及使用体验
|
Fixes #6638
Fix #3790
Fix #4226
Fix #3909
一、PR 概述
在现有
openai_embedding适配器基础上,新增智谱、火山引擎、Ollama 三个 Embedding 预设配置,并改进 URL 处理逻辑以正确兼容带路径的 API 地址。二、背景 / 动机
openai_embedding的局限:URL 处理逻辑基于简单的endswith("/v1")/endswith("/v4")后缀匹配,无法正确处理智谱/api/paas/v4、火山/api/v3等带路径的地址(火山会被错误追加/v1)。同时 proxy 的httpx.AsyncClient未在terminate()中关闭。三、改动内容
OpenAIEmbeddingProvider的 URL 处理:使用urlsplit解析 URL,只在纯域名(无路径)时自动追加/v1,带路径的地址保持原样terminate()中关闭 proxy 的_http_clientopenai_embedding类型,无需新增适配器):embedding-3,https://open.bigmodel.cn/api/paas/v4doubao-embedding-vision,https://ark.cn-beijing.volces.com/api/v3embeddinggemma,本地http://127.0.0.1:11434providerHint改为动态匹配所有 embedding 类型的 hint key,避免显示原始 i18n keyopenai_embedding的提示说明改动类型:
四、实现说明
openai_embedding适配器,通过预设配置(默认embedding_api_base和embedding_model)实现不同服务的快捷接入。urlsplit),而非引入新的openai_compatible_embedding类型,避免代码冗余openai_embedding类型,所有预设共享openai_embedding的embedding_api_base字段提示dimensions参数仍然通过现有的embedding_dimensions字段自动透传,不需要额外的send_dimensions_param开关astrbot/core/provider/sources/openai_embedding_source.py— 改进 URL 处理 + 修复资源泄漏astrbot/core/config/default.py— 新增三个预设配置dashboard/src/components/shared/AstrBotConfig.vue— 改进 hint 处理逻辑dashboard/src/i18n/locales/*/features/config-metadata.json— 三语 i18ndocs/*/use/knowledge-base.md— 文档更新五、行为变化(对外影响)
openai_embedding对带路径的 URL 处理不正确openai_embedding的 URL 处理更加智能openai_embedding配置继续正常工作。纯域名的 URL 仍会自动追加/v1(与之前行为一致)。六、测试与验证
自测结果:
tests/test_dashboard.py— 22 passed)测试命令:
七、Checklist